// MegaDrawerVOD.cpp : implementation file
//

#include "stdafx.h"
#include "vc10.h"
#include "MegaDrawerVOD.h"
#include "../RMSLib/Registry.h"
#include "../GUI/Messages.h"

extern CVC10App theApp;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerVOD
#define STRING_PARAM	"%s %s %s %s"

#define IDC_LIST		1000
#define IDC_CONNECT		1001
#define IDC_DATE_FROM	1002
#define IDC_DATE_TO		1003

CMegaDrawerVOD::CMegaDrawerVOD()
{
}

CMegaDrawerVOD::~CMegaDrawerVOD()
{
}

#ifndef _JNC_REC_

void CMegaDrawerVOD::SetBitmaps(UINT uiSkinSingle, UINT uiSkinDouble, UINT uiSkinSingleUn, UINT uiSkinDoubleUn)
{
	CMegaDrawer::SetBitmaps(uiSkinSingle, uiSkinDouble, uiSkinSingleUn, uiSkinDoubleUn);

	// create controls
	CRect rc(80, 10, 94, 24);
	m_List.SetBitmaps(IDB_ICON_SAVED_WHITE, IDB_ICON_SAVED_GRAY);
	m_List.Create(
		WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_BORDER|
		LBS_OWNERDRAWFIXED|LBS_HASSTRINGS|LBS_NOTIFY, rc, this, IDC_LIST);

	m_btnConnect.Create("", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rc, this, IDC_CONNECT);
	m_btnConnect.SetBitmaps(IDB_CONNECT_OUT, IDB_CONNECT_DOWN, IDB_CONNECT_OVER);
	m_btnConnect.SetBackgroundBmp(m_pSkinSingle);

	DWORD dwStyle = WS_CHILD|WS_TABSTOP|ES_LEFT|ES_AUTOHSCROLL|ES_NUMBER;
	m_DateFrom.Create	(dwStyle, rc, this, IDC_DATE_FROM);
	m_DateTo.Create		(dwStyle, rc, this, IDC_DATE_TO);

	// set initial interval to [yesterday; today]
	CTime tmTime = CTime::GetCurrentTime();
	CString strDateTo = tmTime.Format("%Y %m %d %H %M");
	tmTime -= CTimeSpan(1, 0, 0, 0);
	CString strDateFrom = tmTime.Format("%Y %m %d %H %M");

	m_DateFrom.SetWindowText(strDateFrom);
	m_DateTo.SetWindowText(strDateTo);


	// after control's creation finished - repostiion them
	RepositionControls();
}

void CMegaDrawerVOD::RepositionControls()
{
	// reposition controls
	if(m_bActive)
	{
		if(m_nSize == 1)
		{
			m_List.SetWindowPos(NULL, 4, 20, 320, 120, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateFrom.SetWindowPos(NULL, 35, 155, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateTo.SetWindowPos(NULL, 188, 155, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_btnConnect.SetWindowPos(NULL, 236, 190, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
		}
		else
		{
			m_List.SetWindowPos(NULL, 66, 8, 320, 92, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateFrom.SetWindowPos(NULL, 464, 9, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateTo.SetWindowPos(NULL, 464, 33, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_btnConnect.SetWindowPos(NULL, 561, 57, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
		}
	}
	else
	// if drawer unactive - hide all controls
	{
		m_List.ShowWindow(SW_HIDE);
		m_DateFrom.ShowWindow(SW_HIDE);
		m_DateTo.ShowWindow(SW_HIDE);
		m_btnConnect.ShowWindow(SW_HIDE);
	}
}

void CMegaDrawerVOD::SetSites(map<CString, CSite> * pSites)
{
	m_pSites = pSites;

	// clear list
	m_List.ResetContent();
	// set initial interval to [yesterday; today]
	CTime tmTime = CTime::GetCurrentTime();
	CString strDateTo = tmTime.Format("%Y %m %d %H %M");
	tmTime -= CTimeSpan(1, 0, 0, 0);
	CString strDateFrom = tmTime.Format("%Y %m %d %H %M");
	m_DateFrom.SetWindowText(strDateFrom);
	m_DateTo.SetWindowText(strDateTo);

	// refill list
	map<CString,CSite>::const_iterator s;
	map<CString,CBuilding>::const_iterator b;
	map<CString,CRoom>::const_iterator r;
	map<CString,CInfo>::const_iterator c;

	for(s = m_pSites->begin(); s != m_pSites->end(); s++)
	{
		for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
		{
			for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
			{
				for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
				{
					char name[1024];
					sprintf(name, STRING_PARAM, (LPCSTR) s->first, (LPCSTR) b->first, (LPCSTR) r->first, (LPCSTR) c->first);
					m_List.AddString(name);
				}
			}
		}
	}

	// retrieve last connection params from registry
	char str[1024]={0}, s1[1024]={0}, s2[1024]={0}, s3[1024]={0}, s4[1024]={0};
	REGISTRY_LoadString(KEY_LAST_SITE_CONNECTED, str, 1024);
	sscanf(str, STRING_PARAM, s1, s2, s3, s4);
	sprintf(str, STRING_PARAM, s1, s2, s3, s4);
	m_List.SelectString(-1, str);
}

void CMegaDrawerVOD::SelectCamera(char * pszCameraSign)
{
	int idx = m_List.FindStringExact(-1, pszCameraSign);
	m_List.SetCurSel(idx);
}

CString CMegaDrawerVOD::GetSelectedSite()
{
	return m_strSite;
}

CString CMegaDrawerVOD::GetSelectedBuilding()
{
	return m_strBuilding;
}

CString CMegaDrawerVOD::GetSelectedRoom()
{
	return m_strRoom;
}

CString CMegaDrawerVOD::GetSelectedCamera()
{
	return m_strCamera;
}

CString CMegaDrawerVOD::GetDateFrom()
{
	return m_strDateFrom;
}

CString CMegaDrawerVOD::GetDateTo()
{
	return m_strDateTo;
}

BEGIN_MESSAGE_MAP(CMegaDrawerVOD, CMegaDrawer)
	//{{AFX_MSG_MAP(CMegaDrawerVOD)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_LBN_DBLCLK(IDC_LIST, OnAddressDblClick)
	ON_LBN_SELCHANGE(IDC_LIST, OnCameraChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerVOD message handlers
void CMegaDrawerVOD::OnConnect()
{
	int idx = m_List.GetCurSel();
	if(idx != LB_ERR)
	{
		CString str;
		m_List.GetText(idx, str);

		sscanf(str, STRING_PARAM, m_strSite, m_strBuilding, m_strRoom, m_strCamera);
		m_DateFrom.GetWindowText(m_strDateFrom);
		m_DateTo.GetWindowText(m_strDateTo);

		// store last connection params into registry
		// find full camera info to store it into register
		CSite site = m_pSites->find(m_strSite)->second;
		CBuilding building = site.m_Buildings.find(m_strBuilding)->second;
		CRoom room = building.m_Rooms.find(m_strRoom)->second;
		CInfo camera = room.m_Cameras.find(m_strCamera)->second;
		char pszRegString[4000];
		sprintf(
			pszRegString, 
			"%s %s %s %s %i %s %s %i %i %i %i %i %i",
			(LPCSTR) m_strSite, (LPCSTR) m_strBuilding, (LPCSTR) m_strRoom, (LPCSTR) m_strCamera, 
			camera.m_iID, camera.m_strPassword, camera.m_ip, camera.m_port,
			camera.m_iFrameRate, camera.m_iQualityRange, camera.m_iBitrate, 
			camera.m_iWidth, camera.m_iHeight);

		REGISTRY_SaveString(KEY_LAST_SITE_CONNECTED, pszRegString);

		GetParent()->SendMessage(WM_CONNECT_STORED_SITE);
	}
}

void CMegaDrawerVOD::OnAddressDblClick()
{
	OnCameraChange();
	OnConnect();
}

void CMegaDrawerVOD::OnCameraChange()
{
	int idx = m_List.GetCurSel();
	char buf[1024];
	m_List.GetText(idx, buf);
	// send message to parent
	GetParent()->SendMessage(
		WM_CAMERA_SELECTED, 
		(WPARAM) GetDlgCtrlID(), 
		(LPARAM) buf);
}

#else //ndef _JNC_REC_

#include "../RMSLib/clientinterface.h"

#define IDC_RECORDS		1004
#define IDC_SEARCH		1005

void CMegaDrawerVOD::SetBitmaps(UINT uiSkinSingle, UINT uiSkinDouble, UINT uiSkinSingleUn, UINT uiSkinDoubleUn)
{
	CMegaDrawer::SetBitmaps(uiSkinSingle, uiSkinDouble, uiSkinSingleUn, uiSkinDoubleUn);

	// create controls
	CRect rc(0, 150, 94, 24);
	m_List.SetBitmap(IDB_ICON_WHITE);
	m_List.Create(WS_CHILD|WS_TABSTOP|WS_VSCROLL|CBS_OWNERDRAWFIXED|
		CBS_HASSTRINGS|CBS_AUTOHSCROLL|CBS_DROPDOWNLIST, rc, this, IDC_LIST);

	m_Records.Create(WS_CHILD|WS_TABSTOP|WS_VSCROLL|CBS_OWNERDRAWFIXED|
		CBS_HASSTRINGS|CBS_AUTOHSCROLL|CBS_DROPDOWNLIST, rc, this, IDC_RECORDS);

	m_btnGetRec.Create("", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rc, this, IDC_SEARCH);
	m_btnConnect.Create("", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rc, this, IDC_CONNECT);
	m_btnGetRec.SetBitmaps(IDB_SEARCH_OUT, IDB_SEARCH_DOWN, IDB_SEARCH_OVER);
	m_btnConnect.SetBitmaps(IDB_CONNECT_OUT, IDB_CONNECT_DOWN, IDB_CONNECT_OVER);
	m_btnGetRec.SetBackgroundBmp(m_pSkinSingle);
	m_btnConnect.SetBackgroundBmp(m_pSkinSingle);

	DWORD dwStyle = WS_CHILD|WS_TABSTOP|ES_LEFT|ES_AUTOHSCROLL|ES_NUMBER;
	m_DateFrom.Create	(dwStyle, rc, this, IDC_DATE_FROM);
	m_DateTo.Create		(dwStyle, rc, this, IDC_DATE_TO);

	initFromTo();

	// after control's creation finished - repostiion them
	RepositionControls();
}

void CMegaDrawerVOD::initFromTo()
{
	// set initial interval to [yesterday; today]
	CTime tmTime = CTime::GetCurrentTime();
	tmTime += CTimeSpan(0, 0, 0, 1);
	CString strDateTo = tmTime.Format("%Y %m %d %H %M");
	tmTime -= CTimeSpan(1, 0, 0, 0);
	CString strDateFrom = tmTime.Format("%Y %m %d %H %M");
	m_DateFrom.SetWindowText(strDateFrom);
	m_DateTo.SetWindowText(strDateTo);
}

void CMegaDrawerVOD::RepositionControls()
{
	// reposition controls
	if(m_bActive)
	{
		if(m_nSize == 1)
		{
			m_List.SetWindowPos(NULL, 3, 40, 320, 160, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateFrom.SetWindowPos(NULL, 35, 93, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateTo.SetWindowPos(NULL, 188, 93, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_Records.SetWindowPos(NULL, 3, 138, 320, 160, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_btnGetRec.SetWindowPos(NULL, 10, 190, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_btnConnect.SetWindowPos(NULL, 236, 190, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
		}
		else
		{
			m_List.SetWindowPos(NULL, 3, 23, 320, 160, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateFrom.SetWindowPos(NULL, 35, 73, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_DateTo.SetWindowPos(NULL, 188, 73, 125, 20, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_Records.SetWindowPos(NULL, 329, 23, 320, 160, SWP_NOZORDER|SWP_SHOWWINDOW);
			m_btnGetRec.SetWindowPos(NULL, 340, 65, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
			m_btnConnect.SetWindowPos(NULL, 557, 65, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_SHOWWINDOW);
		}
	}
	else
	// if drawer unactive - hide all controls
	{
		m_List.ShowWindow(SW_HIDE);
		m_DateFrom.ShowWindow(SW_HIDE);
		m_DateTo.ShowWindow(SW_HIDE);
		m_Records.ShowWindow(SW_HIDE);
		m_btnConnect.ShowWindow(SW_HIDE);
		m_btnGetRec.ShowWindow(SW_HIDE);
	}
}

void CMegaDrawerVOD::SetSites(map<CString, CSite> * pSites)
{
	m_pSites = pSites;

	// clear list
	m_List.ResetContent();
	m_Records.ResetContent();
	initFromTo();

	// refill list
	map<CString,CSite>::const_iterator s;
	map<CString,CBuilding>::const_iterator b;
	map<CString,CRoom>::const_iterator r;
	map<CString,CInfo>::const_iterator c;

	for(s = m_pSites->begin(); s != m_pSites->end(); s++)
	{
		for(b = (s->second).m_Buildings.begin(); b != (s->second).m_Buildings.end(); b++)
		{
			for(r = (b->second).m_Rooms.begin(); r != (b->second).m_Rooms.end(); r++)
			{
				for(c = (r->second).m_Cameras.begin(); c != (r->second).m_Cameras.end(); c++)
				{
					char name[1024];
					sprintf(name, STRING_PARAM, (LPCSTR) s->first, (LPCSTR) b->first, (LPCSTR) r->first, (LPCSTR) c->first);
					m_List.AddString(name);
				}
			}
		}
	}

	m_List.RecalcMinWidth();
	m_Records.RecalcMinWidth();

	// retrieve last connection params from registry
	char str[1024]={0}, s1[1024]={0}, s2[1024]={0}, s3[1024]={0}, s4[1024]={0};
	REGISTRY_LoadString(KEY_LAST_SITE_CONNECTED, str, 1024);
	sscanf(str, STRING_PARAM, s1, s2, s3, s4);
	sprintf(str, STRING_PARAM, s1, s2, s3, s4);
	if(LB_ERR != m_List.SelectString(-1, str))
		OnCameraChange();
}

void CMegaDrawerVOD::resetRecords(TimeMark* p, DWORD dwLen)
{
	m_Records.ResetContent();

	CString record;

	int recs = dwLen / (2*sizeof(TimeMark));
	for(int i = 0; i < recs; i++)
	{
		record = p[i*2].toString();
		record += " -- ";
		record += p[i*2+1].toString();
		m_Records.AddString(record);
	}

	m_Records.RecalcMinWidth();
	m_Records.SetCurSel(0);
}

void CMegaDrawerVOD::SelectCamera(char * pszCameraSign)
{
	int idx = m_List.FindStringExact(-1, pszCameraSign);
	m_List.SetCurSel(idx);
}

CString CMegaDrawerVOD::GetSelectedSite()
{
	return m_strSite;
}

CString CMegaDrawerVOD::GetSelectedBuilding()
{
	return m_strBuilding;
}

CString CMegaDrawerVOD::GetSelectedRoom()
{
	return m_strRoom;
}

CString CMegaDrawerVOD::GetSelectedCamera()
{
	return m_strCamera;
}

CString CMegaDrawerVOD::GetDateFrom()
{
	return m_strDateFrom;
}

CString CMegaDrawerVOD::GetDateTo()
{
	return m_strDateTo;
}

CString CMegaDrawerVOD::GetExactDateFrom()
{
	int idz = m_Records.GetCount();
	int idy = m_Records.GetCurSel();
	if((idz == CB_ERR) || (idz == 0) || (idy == CB_ERR))
		return "";

	CString str;
	m_Records.GetLBText(idy, str);

	return str.Left(4+1+2+1+2+1+2+1+2+1+2);
}

CString CMegaDrawerVOD::GetExactDateTo()
{
	int idz = m_Records.GetCount();
	int idy = m_Records.GetCurSel();
	if((idz == CB_ERR) || (idz == 0) || (idy == CB_ERR))
		return "";

	CString str;
	m_Records.GetLBText(idy, str);

	return str.Right(4+1+2+1+2+1+2+1+2+1+2);
}

BEGIN_MESSAGE_MAP(CMegaDrawerVOD, CMegaDrawer)
	//{{AFX_MSG_MAP(CMegaDrawerVOD)
	ON_BN_CLICKED(IDC_SEARCH, OnSearch)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_CBN_SELENDOK(IDC_LIST, OnCameraChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaDrawerVOD message handlers
void CMegaDrawerVOD::OnSearch()
{
	m_Records.ResetContent();
	m_Records.RecalcMinWidth();

	int idx = m_List.GetCurSel();
	if(idx != CB_ERR)
	{
		CString str;
		m_List.GetLBText(idx, str);

		sscanf(str, STRING_PARAM, m_strSite, m_strBuilding, m_strRoom, m_strCamera);
		m_DateFrom.GetWindowText(m_strDateFrom);
		m_DateTo.GetWindowText(m_strDateTo);

		// store last connection params into registry
		// find full camera info to store it into register
		CSite site = m_pSites->find(m_strSite)->second;
		CBuilding building = site.m_Buildings.find(m_strBuilding)->second;
		CRoom room = building.m_Rooms.find(m_strRoom)->second;
		CInfo camera = room.m_Cameras.find(m_strCamera)->second;
		char pszRegString[4000];
		sprintf(
			pszRegString, 
			"%s %s %s %s %i %s %s %i %i %i %i %i %i",
			(LPCSTR) m_strSite, (LPCSTR) m_strBuilding, (LPCSTR) m_strRoom, (LPCSTR) m_strCamera, 
			camera.m_iID, camera.m_strPassword, camera.m_ip, camera.m_port,
			camera.m_iFrameRate, camera.m_iQualityRange, camera.m_iBitrate, 
			camera.m_iWidth, camera.m_iHeight);

		REGISTRY_SaveString(KEY_LAST_SITE_CONNECTED, pszRegString);

		GetParent()->SendMessage(WM_CONNECT_STORED_SITE, 1);
	}
	else
	{
		GetParent()->SendMessage(WM_UPDATE_SESSION_STATUS, (WPARAM)NO_RECORDS);
	}
}

void CMegaDrawerVOD::OnConnect()
{
	int idz = m_Records.GetCount();
	int idy = m_Records.GetCurSel();
	int idx = m_List.GetCurSel();
	if((idz == CB_ERR) || (idz == 0) || (idy == CB_ERR) || (idx == CB_ERR))
	{
		GetParent()->SendMessage(WM_UPDATE_SESSION_STATUS, (WPARAM)NO_RECORDS);
		return;
	}

	CString str;
	m_List.GetLBText(idx, str);

	sscanf(str, STRING_PARAM, m_strSite, m_strBuilding, m_strRoom, m_strCamera);
	m_DateFrom.GetWindowText(m_strDateFrom);
	m_DateTo.GetWindowText(m_strDateTo);

	// store last connection params into registry
	// find full camera info to store it into register
	CSite site = m_pSites->find(m_strSite)->second;
	CBuilding building = site.m_Buildings.find(m_strBuilding)->second;
	CRoom room = building.m_Rooms.find(m_strRoom)->second;
	CInfo camera = room.m_Cameras.find(m_strCamera)->second;
	char pszRegString[4000];
	sprintf(
		pszRegString, 
		"%s %s %s %s %i %s %s %i %i %i %i %i %i",
		(LPCSTR) m_strSite, (LPCSTR) m_strBuilding, (LPCSTR) m_strRoom, (LPCSTR) m_strCamera, 
		camera.m_iID, camera.m_strPassword, camera.m_ip, camera.m_port,
		camera.m_iFrameRate, camera.m_iQualityRange, camera.m_iBitrate, 
		camera.m_iWidth, camera.m_iHeight);

	REGISTRY_SaveString(KEY_LAST_SITE_CONNECTED, pszRegString);

	GetParent()->SendMessage(WM_CONNECT_STORED_SITE, 0);
}

void CMegaDrawerVOD::OnCameraChange()
{
	int idx = m_List.GetCurSel();
	if(idx == CB_ERR)	// addressbook is empty
	{
		m_List.SetWindowText("");
		m_Records.SetWindowText("");
		return;
	}

	char buf[1024];
	m_List.GetLBText(idx, buf);

	m_Records.ResetContent();
	m_Records.RecalcMinWidth();

	// send message to parent
	GetParent()->SendMessage(
		WM_CAMERA_SELECTED, 
		(WPARAM) GetDlgCtrlID(), 
		(LPARAM) buf);
}

#endif //ndef _JNC_REC_
